//
//  LGFamliyTreeHomeViewController.m
//  ligenyun
//
//  Created by Mac on 2018/4/9.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGFamliyTreeHomeViewController.h"
#import "LGFamilyHomeTreeCollectionViewCell.h"
#import "UICollectionView+STNoresult.h"
#import "LGFamlyTreeDetailViewController.h"
#import "LGFamilyMinTreeViewController.h"
@interface LGFamliyTreeHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) UICollectionView                     *collectionView;
@property(nonatomic, strong) STSegementButton                     *segementButton;
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation LGFamliyTreeHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"家庭树";
    self.view.backgroundColor = [UIColor whiteColor];
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, 49)
                                                         andTitle:@[@"最新",@"最火",@"最有趣",@"我的"]
                                                           handle:^(STSegementButton *sender) {
                                                               
                                                               [weakSelf.collectionView.mj_header beginRefreshing];
                                                           }];
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = TM_redColor;
    self.segementButton.butTitleSelectedColor = TM_redColor;
    self.segementButton.butTitleColor = TM_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 10;
    flow.sectionInset = UIEdgeInsetsMake(0, 14, 0, 14);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.segementButton.bottom, UIScreenWidth, [TMUtils tabBarTop] - self.segementButton.bottom + 49) collectionViewLayout:flow];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[LGFamilyHomeTreeCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:self.collectionView];

    self.automaticallyAdjustsScrollViewInsets = NO;
    if (IOS11 && [self.collectionView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        [self.collectionView setContentInsetAdjustmentBehavior:@(2)];
    }
    self.collectionView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, 300, 300) title:@"" buttonTitle:@"创建" buttonHandle:^(NSString *titleString) {
        [weakSelf.navigationController pushViewController:[LGFamilyMinTreeViewController new] animated:YES];
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendTreeRequest];
    }];
    [self.collectionView.mj_header beginRefreshing];
}
#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWith = (UIScreenWidth - 30 - 10)/2;
    return CGSizeMake(itemWith, [LGFamilyHomeTreeCollectionViewCell cellHeight]);
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LGFamilyHomeTreeCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    return item;
}
#pragma --mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:[LGFamlyTreeDetailViewController new] animated:YES];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendTreeRequest{
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        if (self.segementButton.cureentIndex != 3) {
            self.dataSouce = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];
        }else{
            self.dataSouce = nil;
        }
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    }];
}
@end
